package cn.edu.cdut.controller;

import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.service.StuInfoService;
import cn.edu.cdut.service.ScoreInfoService;
import cn.edu.cdut.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * Author  kobe
 * Created on 2017-05-25-12:03
 */
@RestController
public class FileUploadController {

        @Autowired
        StuInfoService stuInfoService;

        @Autowired
        ScoreInfoService scoreInfoService;

        @Autowired
        UploadService uploadService;

        /**
         * 文件上传具体实现方法（单文件上传）
         * @param file;
         *
         */
        @PostMapping("/uploadStuExcel")
        public String uploadStuExcel(@RequestParam("file") MultipartFile file) throws IOException {
                if (!file.isEmpty()) {
                        uploadService.uploadFlie(file);
                        stuInfoService.readStuInfoExcel(file.getOriginalFilename());
                        return "上传成功.";
                }else {
                        return "上传的文件是空的.";
                }
        }

        @PostMapping("/uploadScoreExcel")
        public String uploadScoreExcel(@RequestParam("file") MultipartFile file) throws IOException {
                if (!file.isEmpty()) {
                        uploadService.uploadFlie(file);
                        scoreInfoService.readScoreInfo(file.getOriginalFilename());
                        return "上传成功.";
                }else {
                        return "上传的文件是空的.";
                }
        }

        //查
        @GetMapping("/getAllStuFileInfo")
        public MyRes getAllStuFileInfo() {
                return uploadService.getAllStuFileInfo();
        }

        //查
        @GetMapping("/getAllScoreFileInfo")
        public MyRes getAllScoreFileInfo() {
                return uploadService.getAllScoreFileInfo();
        }

}
